#version 130

uniform sampler2D tex;

in vec2 fragmentTexCoord;
out vec4 fragColor;

const int width = 6;
const int height = 8;

const float char0[48] = float[]( // Space ( )
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0);

const float char1[48] = float[]( // Dot (.)
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 1, 0, 0);

const float char2[48] = float[]( // Colon (:)
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0);

const float char3[48] = float[]( // Comma (,)
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 1, 0, 0, 0);

const float char4[48] = float[]( // Semicolon (;)
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 1, 0, 0, 0);

const float char5[48] = float[]( // Plus (+)
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 1, 1, 1, 1, 1,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 0, 0, 0);

const float char6[48] = float[]( // i (i)
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 1, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 1, 1, 1, 0);

const float char7[48] = float[]( // j (j)
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 1, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 1, 0, 0, 0);

const float char8[48] = float[]( // t (t)
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 1, 1, 1, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 1, 0);

const float char9[48] = float[]( // f (f)
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 1, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 1, 1, 1, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0,
	0, 0, 0, 1, 0, 0);

const float char10[48] = float[]( // L (L)
	0, 0, 0, 0, 0, 0,
	0, 1, 0, 0, 0, 0,
	0, 1, 0, 0, 0, 0,
	0, 1, 0, 0, 0, 0,
	0, 1, 0, 0, 0, 0,
	0, 1, 0, 0, 0, 0,
	0, 1, 0, 0, 0, 0,
	0, 1, 1, 1, 1, 1);

const float char11[48] = float[]( // G (G)
	0, 0, 0, 0, 0, 0,
	0, 0, 1, 1, 1, 1,
	0, 1, 0, 0, 0, 0,
	0, 1, 0, 0, 0, 0,
	0, 1, 0, 0, 0, 0,
	0, 1, 0, 0, 1, 1,
	0, 1, 0, 0, 0, 1,
	0, 0, 1, 1, 1, 1);

const float char12[48] = float[]( // D (D)
	0, 0, 0, 0, 0, 0,
	0, 1, 1, 1, 1, 0,
	0, 1, 0, 0, 0, 1,
	0, 1, 0, 0, 0, 1,
	0, 1, 0, 0, 0, 1,
	0, 1, 0, 0, 0, 1,
	0, 1, 0, 0, 0, 1,
	0, 1, 1, 1, 1, 0);

const float char13[48] = float[]( // K (K)
	0, 0, 0, 0, 0, 0,
	0, 1, 0, 0, 0, 1,
	0, 1, 0, 0, 1, 0,
	0, 1, 0, 1, 0, 0,
	0, 1, 1, 0, 0, 0,
	0, 1, 0, 1, 0, 0,
	0, 1, 0, 0, 1, 0,
	0, 1, 0, 0, 0, 1);

const float char14[48] = float[]( // W (W)
	0, 0, 0, 0, 0, 0,
	0, 1, 0, 0, 0, 1,
	0, 1, 0, 0, 0, 1,
	0, 1, 0, 0, 0, 1,
	0, 1, 0, 1, 0, 1,
	0, 1, 0, 1, 0, 1,
	0, 1, 1, 0, 1, 1,
	0, 1, 0, 0, 0, 1);

const float char15[48] = float[]( // Sharp (#)
	0, 0, 0, 0, 0, 0,
	0, 0, 1, 0, 1, 0,
	0, 0, 1, 0, 1, 0,
	0, 1, 1, 1, 1, 1,
	0, 0, 1, 0, 1, 0,
	0, 1, 1, 1, 1, 1,
	0, 0, 1, 0, 1, 0,
	0, 0, 1, 0, 1, 0);

void main(void)
{
	vec2 pixelSize = vec2(1, 1) / textureSize(tex, 0);
	vec2 offset = fragmentTexCoord / pixelSize;
	ivec2 index = ivec2(floor(offset / vec2(width, height))) * ivec2(width, height);

	vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
	for (int i = 0; i < height; ++i)
		for (int j = 0; j < width; ++j)
			color += texture(tex, pixelSize * (index + ivec2(i, j)));

	color /= (height * width);
	int Y = int(dot(color, vec4(0.299, 0.587, 0.114, 0.0)) * 15.9);

	ivec2 coord = ivec2(mod(offset, vec2(width, height)));
	if (Y == 0) {
		color *= char0[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 1) {
		color *= char1[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 2) {
		color *= char2[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 3) {
		color *= char3[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 4) {
		color *= char4[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 5) {
		color *= char5[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 6) {
		color *= char6[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 7) {
		color *= char7[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 8) {
		color *= char8[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 9) {
		color *= char9[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 10) {
		color *= char10[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 11) {
		color *= char11[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 12) {
		color *= char12[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 13) {
		color *= char13[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 14) {
		color *= char14[(height-coord.y-1)*width + coord.x];
	}
	else if (Y == 15) {
		color *= char15[(height-coord.y-1)*width + coord.x];
	}

	fragColor = color;
	return;
}

